package ace.module.captcha.sms.core.impl.service.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.core.utils.SystemClockUtils;
import ace.module.captcha.sms.api.constant.CaptchaSmsConstant;
import ace.module.captcha.sms.api.enums.CaptchaSmsApplicationStatusEnum;
import ace.module.captcha.sms.core.impl.dao.entity.CaptchaSmsApplication;
import ace.module.captcha.sms.core.impl.dao.entity.CaptchaSmsApplicationExtend;
import ace.module.captcha.sms.core.impl.dao.entity.CaptchaSmsItemConfig;
import ace.module.captcha.sms.core.impl.dao.repository.CaptchaSmsApplicationExtendRepository;
import ace.module.captcha.sms.core.impl.dao.repository.CaptchaSmsApplicationRepository;
import ace.module.captcha.sms.core.impl.dao.repository.CaptchaSmsItemConfigRepository;
import ace.module.captcha.sms.core.impl.service.CaptchaSmsApplicationService;
import ace.module.sms.api.constant.SmsConstant;
import java.util.concurrent.TimeUnit;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Caspar
 * @create 2020/11/5
 */
@Slf4j
@AllArgsConstructor
@Component
public class CaptchaSmsApplicationServiceImpl implements CaptchaSmsApplicationService {

  private final CaptchaSmsApplicationRepository captchaSmsApplicationRepository;
  private final CaptchaSmsItemConfigRepository captchaSmsItemConfigRepository;
  private final CaptchaSmsApplicationExtendRepository captchaSmsApplicationExtendRepository;


  @Override
  public CaptchaSmsApplication getOperatorPlatformApp() {
    return this.captchaSmsApplicationRepository.getById(CaptchaSmsConstant.OPERATOR_PLATFORM_SMS_APPLICATION_ID);
  }

  @Transactional(rollbackFor = Exception.class)
  @Override
  public Long initOperatorPlatformApp() {
    if (captchaSmsApplicationRepository.getById(CaptchaSmsConstant.OPERATOR_PLATFORM_SMS_APPLICATION_ID) != null) {
      log.info("运营管理平台-短信验证码应用 已经存在 不再初始化");
      return CaptchaSmsConstant.OPERATOR_PLATFORM_SMS_APPLICATION_ID;
    }

    CaptchaSmsApplication captchaSmsApplication = CaptchaSmsApplication.builder()
        .id(CaptchaSmsConstant.OPERATOR_PLATFORM_SMS_APPLICATION_ID)
        .key(CaptchaSmsConstant.OPERATOR_PLATFORM_SMS_APPLICATION_KEY)
        .name("运营管理平台-短信验证码应用")
        .remark(StringUtils.EMPTY)
        .status(CaptchaSmsApplicationStatusEnum.NORMAL.getCode())

        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(SystemClockUtils.currentTimeMillis())
        .updateTime(SystemClockUtils.currentTimeMillis())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();

    CaptchaSmsItemConfig config = CaptchaSmsItemConfig.builder()
        .id(CaptchaSmsConstant.OPERATOR_PLATFORM__CAPTCHA_SMS_ITEM_CONFIG_ID)
        .name("运营管理平台-短信验证码应用")
        .remark(StringUtils.EMPTY)
        .attemptCount(3)
        .codeLength(4)
        .codeString("0123456789")
        .enable(true)
        .enableSystemCode(false)
        .expireInSeconds(TimeUnit.SECONDS.convert(5, TimeUnit.MINUTES))
        .sendIntervalInSeconds(60L)
        .systemCode("8888")

        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(SystemClockUtils.currentTimeMillis())
        .updateTime(SystemClockUtils.currentTimeMillis())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();

    CaptchaSmsApplicationExtend captchaSmsApplicationExtend =
        CaptchaSmsApplicationExtend.builder()
            .id(captchaSmsApplication.getId())
            .captchaSmsAppId(captchaSmsApplication.getId())
            .captchaSmsItemConfigId(config.getId())
            .smsAppId(SmsConstant.DEFAULT_APP_ID)

            .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
            .createTime(SystemClockUtils.currentTimeMillis())
            .updateTime(SystemClockUtils.currentTimeMillis())
            .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
            .build();

    this.captchaSmsApplicationExtendRepository.insert(captchaSmsApplicationExtend);
    this.captchaSmsApplicationRepository.insert(captchaSmsApplication);
    this.captchaSmsItemConfigRepository.insert(config);

    log.info("运营管理平台-短信验证码应用 初始化成功");
    return CaptchaSmsConstant.OPERATOR_PLATFORM_SMS_APPLICATION_ID;
  }
}
